package island.gotenyama.wand.bean;

import java.beans.PropertyEditorSupport;

/**
 * PropertyEditor For Enum
 *
 * @author tk
 */
public class EnumEditor extends PropertyEditorSupport {

	/**
	 * Clazz
	 */
	@SuppressWarnings("rawtypes")
	private Class clazz;

	/**
	 * Constractor
	 *
	 * @param clazz Target Enum
	 */
	@SuppressWarnings("rawtypes")
	public EnumEditor(Class clazz) {
		this.clazz = clazz;
	};

	@Override
	public String getAsText() {
		return (getValue() == null ? "" : ((Enum<?>) getValue()).name());
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		setValue(Enum.valueOf(clazz, text));
	}
}
